<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>对象遍历相关方法</h1>
    <ul>
        <li>Object.keys(obj)：获取属性名数组</li>
        <li>Object.values(obj)：获取值数组</li>
        <li>Object.entries(obj)：获取键值对数组的数组</li>
        <li>Object.getOwnPropertyNames(obj)：获取属性名数组（包含不可枚举的属性名）</li>
    </ul>
    <script>
        let obj = {
            name: "奉先",
            age: 35,
            sex: "男"
        }
        // 对象可以使用for...in遍历（ES5）
        // 通过属性名遍历
        for (let i in obj) {
            console.log(i,"=",obj[i]);
        }

        console.log("-----------------Object.keys(obj)----------------")
        // 获取属性名的数组
        let keys = Object.keys(obj);
        for (let v of keys) {
            console.log(v,"=",obj[v]);
        }

        console.log("-----------------Object.values(obj)----------------")
        // 获取属性名的数组
        let values = Object.values(obj);
        for (let v of values) {
            console.log(v);
        }

        console.log("-----------------Object.entries(obj)----------------")
        // 获取属性名的数组
        let entries = Object.entries(obj);
        for (let v of entries) {
            console.log(v[0],"=",v[1]);
        }

        console.log("-----------------数组.keys()----------------")
        let arr = [1,2,3,4];
        let keys2 = Object.keys(arr);
        console.log("keys2 = ",keys2); // ["0","1","2","3"]

        let keys3 = Object.getOwnPropertyNames(arr);
        console.log("keys3 = ",keys3); // [ "0", "1", "2", "3", "length" ]

    </script>
</body>
</html>